package com.hmall.item.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;
import com.hmall.common.dto.SearchDTO;
import com.hmall.common.pojo.ItemDoc;
import com.hmall.common.pojo.SearchVo;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.common.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import java.util.stream.Collectors;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {

    @Autowired
    private ItemService itemService;


    @Override
    public PageDTO<Item> getByPage(Integer page, Integer size) {


        Page<Item> page1 = this.lambdaQuery().orderByDesc(Item::getCreateTime).page(Page.of(page,size));
        //使用ItemService中的page方法分页查询
        return new PageDTO<>(page1.getTotal(),page1.getRecords());

    }

    @Override
    public void update1(Item item) {
        this.lambdaUpdate().set(Item::getName, item.getName())
                .set(Item::getCategory, item.getCategory())
                .set(Item::getBrand, item.getBrand())
                .set(Item::getPrice, item.getPrice())
                .set(Item::getStock, item.getStock())
                .set(Item::getSpec, item.getSpec())
                .set(Item::getImage, item.getImage())
                .set(Item::getIsAD, false)
                .set(Item::getUpdateTime, LocalDateTime.now())
                .update();


    }

    @Override
    public void updateStatus(Long id, Integer status) {
        this.lambdaUpdate().set(Item::getStatus,status)
                .set(Item::getUpdateTime,LocalDateTime.now())
                .eq(Item::getId,id)
                .update();
    }

    @Override
    public SearchVo searchFilters() {
        List<Item> list = this.lambdaQuery().select(Item::getCategory).list();
        List<String> category = list.stream().map(Item::getCategory).distinct().collect(Collectors.toList());

        List<String> brand = this.lambdaQuery().select(Item::getBrand).list().stream().map(Item::getBrand).distinct().collect(Collectors.toList());

        SearchVo searchVo = new SearchVo();
        searchVo.setCategory(category);
        searchVo.setBrand(brand);
        return searchVo;
    }

    @Override
    public PageDTO<ItemDoc> searchList(SearchDTO dTO) {
        Page<Item> page = lambdaQuery().select(Item::getId, Item::getName, Item::getPrice, Item::getSold, Item::getCommentCount, Item::getImage)
                .eq(Item::getStatus, 1)
                .like(ObjectUtil.isNotEmpty(dTO.getKey()), Item::getName, dTO.getKey())
                .eq(ObjectUtil.isNotEmpty(dTO.getCategory()), Item::getCategory, dTO.getCategory())
                .eq(ObjectUtil.isNotEmpty(dTO.getBrand()), Item::getBrand, dTO.getBrand())
                .ge(ObjectUtil.isNotEmpty(dTO.getMinPrice()), Item::getPrice, dTO.getMinPrice())
                .le(ObjectUtil.isNotEmpty(dTO.getMaxPricd()), Item::getPrice, dTO.getMaxPricd())
                .page(Page. of(dTO.getPage(), dTO.getSize()));
        List<ItemDoc> itemDocs = BeanUtil.copyToList(page.getRecords(), ItemDoc.class);
        return new PageDTO<>(page.getTotal(),itemDocs);
    }
}
